<template>
  <a-drawer
    :title="formTitle"
    width="35%"
    :label-col="4"
    :wrapper-col="14"
    :visible="open"
    @close="onClose">
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="上级部门" prop="parentId">
        <a-tree-select
          v-model="form.parentId"
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="deptOptions"
          placeholder="请选择"
          :replaceFields="{children:'children', title:'deptName', key:'deptId', value: 'deptId' }"
        >
        </a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="部门名称" prop="deptName" >
        <a-input v-model="form.deptName" placeholder="请输入部门名称" />
      </a-form-model-item>
      <a-form-model-item label="显示顺序" prop="orderNum" >
        <a-input v-model="form.orderNum" placeholder="请输入显示顺序" />
      </a-form-model-item>
      <a-form-model-item label="支持类型" prop="supportTypes" >
        <a-select mode="multiple" v-model="form.supportTypes" placeholder="请选择支持类型" style="width: 100%">
          <a-select-option v-for="(d, index) in hotelTypeOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getDept, addDept, updateDept } from '@/api/platform/dept'

export default {
  name: 'CreateForm',
  props: {
    deptOptions: {
      type: Array,
      required: true
    },
    hotelTypeOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      loading: false,
      formTitle: '',
      // 表单参数
      form: {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        supportType: undefined,
        supportTypes: undefined,
        remark: undefined
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        parentId: [{ required: true, message: '上级部门不能为空', trigger: 'change' }],
        deptName: [{ required: true, message: '部门名称不能为空', trigger: 'change' }],
        orderNum: [{ required: true, message: '部门排序不能为空', trigger: 'change' }],
        supportTypes: [{ required: true, message: '支持类型不能为空', trigger: 'change' }]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        deptId: undefined,
        deptName: undefined,
        orderNum: undefined,
        supportType: undefined,
        remark: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加部门'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const deptId = row ? row.deptId : ids
      getDept(deptId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改部门'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.supportType = this.form.supportTypes.toString()
          if (this.form.deptId !== undefined && this.form.deptId !== null) {
            updateDept(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          } else {
            addDept(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
